const domainName = getApp().globalData.domainName
const parameter = getApp().globalData.parameter
const input_default = {
  show:false,
  method_id:'',
  Test_methods_name:'',
  name:'',
  description:''
}
// pages/sysConfig/Test_types/Test_types.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lis:[],
  },

  set_order(e){
    let index = e.currentTarget.id
    let point_id = this.data.lis[index].id
    wx.showModal({
      editable: true,
      placeholderText: '请输入设定序号',
      showCancel: true,
      title: '设置排序',
      success: res => {
        let order_set = res.content
        if(res.confirm){
          wx.request({
            url: domainName + 'Test_points_update_order_set',
            data: {point_id,order_set},...parameter,
            success: res => {
              if(res.data.update_res){
                wx.showToast({
                  title: '修改完成',
                })
                this.page_init(this.data.content_id)
              }else if(res.data.err_msg){
                wx.showToast({
                  title: res.data.err_msg,
                  icon: 'error',
                })
              }
            },
          })
        }
      },
    })
  },

  set_opt(e){
    let index = e.currentTarget.id
    let point_id = this.data.lis[index].id
    let point_name = this.data.lis[index].name
    let description = this.data.lis[index].description
    wx.showActionSheet({
      itemList: ['修改名称及描述','删除要点','彻底删除要点'],
      alertText: '操作选项',
      success: res => {
        if(res.tapIndex==0){
          let url_data = {
            point_id,
            name:point_name,
            description,
          }
          wx.navigateTo({
            url: '/pages/sysConfig/Test_points_edit/Test_points_edit?url_data=' + encodeURIComponent(JSON.stringify(url_data))
          })
          this.can_onshow = true
        }else if(res.tapIndex==1){
          wx.request({
            url: domainName + 'Test_points_delete',
            data: {point_id},...parameter,
            success: res => {
              if(res.data.update_res){
                wx.showToast({
                  title: '删除完成！',
                })
                this.page_init(this.data.content_id)
              }
            },
          })
        }else if(res.tapIndex==2){
          wx.showModal({
            content: '确定要彻底删除该要点吗？',
            showCancel: true,
            title: '警告！',
            success: res => {
              if(res.confirm){
                wx.request({
                  url: domainName + 'Test_points_delete_completely',
                  data: {point_id},...parameter,
                  success: res => {
                    if(res.data.delete_res){
                      wx.showToast({
                        title: '删除完成！',
                      })
                      this.page_init(this.data.content_id)
                    }
                  },
                })
              }
            },
          })
        }
      },
    })
  },


  confirm_add(){
    wx.showModal({
      content: '确定提交吗？',
      title: '提示',
      success: res => {
        if(res.confirm){
          wx.request({
            url: domainName + 'Test_points_submit',
            data: {
              description:this.data.description,
              point:this.data.point,
              content_id:this.data.content_id,
              method_id:this.data.method_id,
            },...parameter,
            success: res => {
              console.log('confirm_add',res.data)
              if(res.data.row_id){
                wx.showToast({
                  title: '提交完成！',
                })
                this.setData({show:false,name:'',})
                this.page_init(this.data.content_id)
              }
            },
          })
        }
      },
    })
  },
  set_Test_methods(e){
    let index = e.detail.value
    this.setData({
      method_id:this.data.Test_methods_list[index].id,
      Test_methods_name:this.data.Test_methods_list[index].name,
    })
  },

  input_point(e){
    this.setData({
      point:e.detail.value
    })
  },

  input_description(e){
    this.setData({
      description:e.detail.value
    })
  },

  add(){
    this.setData({show:true})
  },

  page_init(content_id){
    wx.request({
      url: domainName + 'Test_points_init',
      data: {content_id},...parameter,
      success: res => {
        console.log('page_init',res.data)
        this.setData({
          lis:res.data.lis,
          Test_methods_list:res.data.Test_methods_list,
          input_default,
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // console.log('options',options)
    this.data.content_id = options.content_id
    this.data.name = options.name
    wx.setNavigationBarTitle({
      title: '要点：' + this.data.name,
    })
    this.page_init(this.data.content_id)
  },

  onReady(){
    this.setData({
      windowHeight:wx.getSystemInfoSync().windowHeight,
    })
  },

  onShow(){
    if(this.can_onshow){
      this.page_init(this.data.content_id)
    }
  }

})